package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    private IDepartmentService departmentService;
    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }


    //查询请求
    @RequestMapping("/list")
    @RequiredPermission(name = "部门列表",expression = "department.list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";
    }

    //删除请求
    @RequestMapping("/delete")
    @RequiredPermission(name = "部门删除",expression = "department.delete")
    public String delete(Long id) {
        if (id != null) {
            departmentService.delete(id);
        }
        //删除以后重定向到查询页面
        return "redirect:/department/list";
    }


    //新增或者修改
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "部门修改/增加",expression = "department.saveOrUpdata")
    public String update(Department department) {
        if (department.getId() == null) {
            //如果id等于空那就新增
            departmentService.insert(department);
        }else {
            //如果id不为空那就去修改
            departmentService.update(department);
        }
        //修改或者插入完以后使用重定向到查询页面
        return "redirect:/department/list";
    }
}
